package com.randi.web.controller.profile;

import com.randi.common.annotation.Anonymous;
import com.randi.common.annotation.LogAnnotation;
import com.randi.common.config.AppConfig;
import com.randi.common.core.domain.Result;
import com.randi.framework.domain.vo.ProfileInfo;
import com.randi.framework.domain.dto.ProfileUpdateDTO;
import com.randi.framework.domain.dto.ProfilePasswordUpdateDTO;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;
import com.randi.file.entity.vo.FileUploadVo;
import com.randi.framework.domain.service.ISysLoginService;
import com.randi.framework.domain.service.ISysProfileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ziye
 */
@RestController
@RequestMapping("/profile")
@Api(value = "profile", tags = "个人业务管理")
public class SysProfileController {

    @Resource
    private ISysProfileService iSysProfileService;

    @Resource
    private ISysLoginService iSysLoginService;

    @Resource
    private AppConfig.SystemConfig.PasswordConfig passwordConfig;

    /**
     * 获取个人信息
     *
     * @return 结果
     */
    @GetMapping
    @ApiOperation(value = "获取个人信息", notes = "")
    public Result getProfileInfo() {
        ProfileInfo userInfo = iSysProfileService.getUserInfo();
        return Result.success().put(userInfo);
    }

    /**
     * 更新个人信息
     *
     * @return Result
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新个人信息", notes = "")
    @LogAnnotation(module = "个人业务管理", value = "更新个人信息", businessType = BusinessType.UPLOAD)
    public Result updateCurrentUser(@RequestBody ProfileUpdateDTO profileUpdateDTO) {
        int status = iSysProfileService.updateLoginUser(profileUpdateDTO);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_EDITOR_ERROR);
        }
        return Result.success();
    }

    /**
     * 更新个人密码
     *
     * @return Result
     */
    @PostMapping("/update/password")
    @ApiOperation(value = "更新个人密码", notes = "")
    @LogAnnotation(module = "个人业务管理", value = "更新个人密码", businessType = BusinessType.UPLOAD)
    public Result updateCurrentUserPassword(@Validated @RequestBody ProfilePasswordUpdateDTO profilePasswordUpdateDto) {
        int status = iSysProfileService.updateLoginUserPassword(profilePasswordUpdateDto);
        if (status == 0) {
            return Result.error(ResultCode.CURRENT_UPDATE_PASSWORD_ERROR);
        }
        if (passwordConfig.getUpdateAgainLogin()) {
            iSysLoginService.logout();
            return Result.success(ResultCode.CURRENT_UPDATE_PASSWORD_SUCCESS_AGAIN_LOGIN);
        }

        return Result.success(ResultCode.CURRENT_UPDATE_PASSWORD_SUCCESS);
    }

    /**
     * 更新个人头像
     *
     * @param fileUpload FileUploadVo
     * @return Result
     * @throws IOException IO异常
     */
    @PostMapping("/update/avatar")
    @ApiOperation(value = "更新用户头像", notes = "")
    @LogAnnotation(module = "个人业务管理", value = "更新个人头像", businessType = BusinessType.UPLOAD)
    public Result updateCurrentAvatar(FileUploadVo fileUpload) throws IOException {
        int status = iSysProfileService.updateLoginUserAvatar(fileUpload);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_EDITOR_ERROR_AVATAR);
        }
        return Result.success();
    }

    /**
     * 头像预览
     *
     * @param response HttpServletResponse
     * @param hash     哈希
     */
    @Anonymous
    @GetMapping("/avatar/preview/{hash}")
    @ApiOperation(value = "头像预览", notes = "")
    public void previewCurrentAvatar(HttpServletResponse response, @PathVariable String hash) {
        iSysProfileService.previewCurrentAvatar(response, hash);
    }

}
